package com.edu.exam.entity.facility;

import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import java.time.LocalDateTime;

/**
 * 教室基础信息实体
 * 所属模块：F-08 场地基础信息管理模块
 * 对应数据库表：venue_classroom
 *
 * 管理教室的基础信息，包括教室编码、名称、容量、设备配置等。
 * 支持教室的分类管理和楼宇关联，提供教室资源的完整视图。
 *
 * @author System
 * @version 1.0.0
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Entity
@Comment("教室基础信息表")
@SQLDelete(sql = "UPDATE venue_classroom SET is_deleted = true, delete_time = CURRENT_TIMESTAMP WHERE id = ?")
@Where(clause = "is_deleted = false")
public class Classroom {

    /**
     * 教室唯一标识符
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false)
    @Comment("教室唯一标识符")
    private Long id;

    /**
     * 教室编码，格式：楼宇编码-楼层-房间号
     */
    @Column(name = "classroom_code", nullable = false, unique = true, length = 50)
    @Comment("教室编码，格式：楼宇编码-楼层-房间号")
    private String classroomCode;

    /**
     * 教室名称
     */
    @Column(name = "classroom_name", nullable = false, length = 200)
    @Comment("教室名称")
    private String classroomName;

    /**
     * 所属分类ID
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "category_id", nullable = false, foreignKey = @ForeignKey(name = "fk_classroom_category"))
    @Comment("所属分类ID")
    private ClassroomCategory category;

    /**
     * 所属楼宇ID
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "building_id", nullable = false, foreignKey = @ForeignKey(name = "fk_classroom_building"))
    @Comment("所属楼宇ID")
    private Building building;

    /**
     * 楼层位置
     */
    @Column(name = "floor_location", nullable = false, length = 50)
    @Comment("楼层位置")
    private String floorLocation;

    /**
     * 座位数
     */
    @Column(name = "seat_count", nullable = false)
    @Comment("座位数")
    private Integer seatCount;

    /**
     * 考试座位数
     */
    @Column(name = "exam_seat_count")
    @Comment("考试座位数")
    private Integer examSeatCount;

    /**
     * 电脑数量
     */
    @Column(name = "computer_count")
    @Comment("电脑数量")
    private Integer computerCount = 0;

    /**
     * 是否配备多媒体设备
     */
    @Column(name = "has_multimedia", nullable = false)
    @Comment("是否配备多媒体设备")
    private Boolean hasMultimedia = false;

    /**
     * 是否配备空调
     */
    @Column(name = "has_air_conditioner", nullable = false)
    @Comment("是否配备空调")
    private Boolean hasAirConditioner = false;

    /**
     * 是否配备投影仪
     */
    @Column(name = "has_projector", nullable = false)
    @Comment("是否配备投影仪")
    private Boolean hasProjector = false;

    /**
     * 是否配备网络
     */
    @Column(name = "has_internet", nullable = false)
    @Comment("是否配备网络")
    private Boolean hasInternet = false;

    /**
     * 管理单位
     */
    @Column(name = "management_unit", length = 100)
    @Comment("管理单位")
    private String managementUnit;

    /**
     * 教室状态
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "status", nullable = false, length = 20)
    @Comment("教室状态")
    private ClassroomStatus status = ClassroomStatus.NORMAL;

    /**
     * 记录创建时间
     */
    @Column(name = "create_time", nullable = false, updatable = false)
    @Comment("记录创建时间")
    private LocalDateTime createTime;

    /**
     * 记录最后更新时间
     */
    @Column(name = "update_time", nullable = false)
    @Comment("记录最后更新时间")
    private LocalDateTime updateTime;

    /**
     * 创建人用户ID
     */
    @Column(name = "create_user")
    @Comment("创建人用户ID")
    private Long createUser;

    /**
     * 最后更新人用户ID
     */
    @Column(name = "update_user")
    @Comment("最后更新人用户ID")
    private Long updateUser;

    /**
     * 乐观锁版本号
     */
    @Version
    @Column(name = "version", nullable = false)
    @Comment("乐观锁版本号")
    private Integer version = 1;

    /**
     * 软删除标记
     */
    @Column(name = "is_deleted", nullable = false)
    @Comment("软删除标记")
    private Boolean isDeleted = false;

    /**
     * 删除时间
     */
    @Column(name = "delete_time")
    @Comment("删除时间")
    private LocalDateTime deleteTime;

    @PrePersist
    protected void onCreate() {
        LocalDateTime now = LocalDateTime.now();
        this.createTime = now;
        this.updateTime = now;
    }

    @PreUpdate
    protected void onUpdate() {
        this.updateTime = LocalDateTime.now();
    }
}